/**
 * 
 */
package com.effectiv.req1.controller;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import jodd.util.StringUtil;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.effectiv.req1.domain.Company;
import com.effectiv.req1.domain.Lov;
import com.effectiv.req1.domain.Project;
import com.effectiv.req1.domain.User;

import com.effectiv.req1.repository.LovRepository;
import com.effectiv.req1.repository.ProjectRepository;
import com.effectiv.req1.repository.UserRepository;
import com.effectiv.req1.security.SecurityUtil;



/**
 * @author Dhrubo
 * 
 */
@Controller
@Slf4j
public class UserController {
	
	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private ProjectRepository projectRepository;
	
	@Autowired
	private LovRepository lovRepository;
	
	@RequestMapping(value = "/listusers")
	public final String showHome(@RequestParam(value="selection" , required=false) boolean selection,@RequestParam(value="projectId", required=false) String projectId, Model model) {
		Company company = SecurityUtil.getUserDetails().getCompany();
		
		model.addAttribute("users", userRepository.findByCompany(company));
		
		model.addAttribute("selection", selection);
		
		Project project = null;
		
		if(StringUtil.isNotBlank(projectId)){
			project = projectRepository.findOne(projectId);
			
			model.addAttribute(project);
			
		}
		
		return "listusers";
	}
	
	@RequestMapping(value = "/newuser")
	public final String newUserDisplay(Model model) {
		
		log.info("Displaying UI to create new user");
		
		User user = new User();
		
		
		model.addAttribute(user);


		return "newuser";
	}
	
	@RequestMapping(value = "/saveuser", method = RequestMethod.POST)
	public final String saveUser(@Valid User user, BindingResult result) {
		
		if (!result.hasErrors()) {
			
			Company company = SecurityUtil.getUserDetails().getCompany();
			
			log.info("Company = {}", company);
			
			user.setCompany(company);
			
			Date timestamp = new Date();
			
			user.setCreatedDate(timestamp);
			user.setLastUpdatedDate(timestamp);
			
			//TODO - change this to generate random string with commons random string
			user.setPassword("salman");
			
			
			Lov role = lovRepository.findByName("ROLE_USER");
			log.debug("Role = {}", role);
			
			List<Lov> roles = new ArrayList<Lov>();
			roles.add(role);
			
			user.setRoles(roles);
			
			userRepository.save(user);
		}
		
		return "newuser";
	}
	
	@RequestMapping(value = "/getcompanyusers")
	public @ResponseBody List<User> getCompanyUsers(@RequestParam("u") String user) {
		
		List<User> users = null;
		log.info("User - {}", user);
		
		if(StringUtil.isEmpty(user)){
			users = userRepository.findByCompany(SecurityUtil.getUserDetails().getCompany());
		}
		else{
			users = userRepository.findByCompanyAndNameLike(SecurityUtil.getUserDetails().getCompany(), user);
		}
		
		log.info("users - {}", users);
		
		return users;
	}
	
}
